<template>
  <div class="regdit">
    <div class="labels">
      <el-button icon="el-icon-s-home" round @click="$router.push('/login')">返回登录</el-button>
    </div>
      <div class="registerLayer">
        <p style="margin: 5px 0 5px 100px;font-family: 'PingFang-RE';
          font-size: 32px;text-align: center;color: #5093d2;letter-spacing: 50px" >注册</p>
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" >
        <el-form-item label="名字" prop="age">
          <el-input v-model.number="ruleForm.name"></el-input>
        </el-form-item>
      <el-form-item label="工号" prop="age">
        <el-input v-model.number="ruleForm.id"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input type="password" v-model="ruleForm.password" ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input type="password" v-model="checkPass" ></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input type="text" v-model="ruleForm.email" style="width: 70%" ></el-input>
        <el-button type="primary" style="margin-left: 5px" @click="sendEmail">发送信息</el-button>
      </el-form-item>
        <el-form-item label="邮箱验证码" prop="chick-name">
          <el-input type="text" v-model="ruleForm.emailNum" style="width: 55% "></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="pass">
          <el-input type="text" v-model="ruleForm.tel" ></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="pass">
          <el-input type="text" v-model="ruleForm.sex" ></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="pass">
          <el-input type="text" v-model="ruleForm.petName" ></el-input>
        </el-form-item>
      <el-form-item label="上传头像" >
        <div class="avatar-uploader">
        <el-upload
            action="http://localhost:8443/api/uploadAvatar"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
        >
          <el-image v-if="ruleForm.imgUrl"  v-bind:src="ruleForm.imgUrl" class="avatar"></el-image>
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" style="margin-right: 60px;" @click="registerUser" >提交</el-button>
        <el-button >重置</el-button>
    </el-form-item>
    </el-form>
      </div>
  </div>
</template>

<script>
export default {
  name: "Regdit",
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      checkPass: '',
      ruleForm: {
        id: '',
        name:'',
        password: '',
        email:'',
        imgUrl:'',
        emailNum:'',
        type:'',
        tel:'',
        sex:'',
        petName:'',
      },
      rules: {
        name: [
          { trigger: 'blur'}
        ],
        password: [
          { trigger: 'blur'}
        ],
        checkPass: [
          {validator: validatePass2, trigger: 'blur'}
        ]
      }
    }
  },
  methods:{
    handleAvatarSuccess(res) {
      this.ruleForm.imgUrl = res
      this.$message.warning('上传成功')
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 1;
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 1MB!');
      }
    },
    sendEmail(){
      this.$axios.post(
          '/sendEmail',{
              email:this.ruleForm.email
          }
      ).then(resp=>{
        if(resp.data.code===200){
          this.$message({
            type: 'success',
            message:'发送成功,有效期5分钟',
          });
          this.Cookies.set('emailNum',resp.data.emailNum,'5min')
        }
        else{
          this.$message({
            type: 'error',
            message:'请检查邮箱格式',
          });
        }
      })
    },
    registerUser(){
      if(this.ruleForm.id===''){
        this.$message({
          type: 'error',
          message:'工号必填',
        });
        return ;
      }
      if(this.ruleForm.password===''){
        this.$message({
          type: 'error',
          message:'密码必填',
        });
        return ;
      }
      if(this.ruleForm.email===''){
        this.$message({
          type: 'error',
          message:'邮箱必填',
        });
        return ;
      }
      if(this.ruleForm.emailNum===''){
        this.$message({
          type: 'error',
          message:'请输入邮箱验证码',
        });
        return ;
      }
      if(this.ruleForm.name===''){
        this.$message({
          type: 'error',
          message:'名字必填',
        });
        return ;
      }
      if(this.ruleForm.emailNum===''||this.ruleForm.emailNum!==this.Cookies.get('emailNum')){
        this.$message({
          type: 'error',
          message:'验证码未填或错误',
        });
        return ;
      }
      this.$axios.post(
          '/userRegister',
          {
            id:this.ruleForm.id,
            name:this.ruleForm.name,
            email:this.ruleForm.email,
            imgUrl:this.ruleForm.imgUrl,
            type:this.ruleForm.type,
            password:this.ruleForm.password,
            emailNum:this.ruleForm.emailNum,
            tel:this.ruleForm.tel,
            sex:this.ruleForm.sex,
            petName:this.ruleForm.petName,
          })
          .then(resp=>{
            if (resp.data.code===200){
              this.$message({
                type: 'success',
                message:'注册成功，请登录',
              });
              this.$router.push('/login')
            }
            else {
              this.$message({
                type: 'error',
                message:'注册失败，邮箱或Id已注册',
              });
            }
            this.Cookies.remove('emailNum')
          })

    }
  }
}
</script>

<style scoped>
.registerLayer {
  padding: 35px 50px 0 10px;
  margin: auto;
  width: 550px;
  border-width:  3px;
  border-style: solid ;
  border-color: #2faf8a;
  height: 850px;
  background-color:white;
  border-radius: 32px;
  position: absolute;
  top:50%;
  left:50%;
  transform: translate(-50%,-50%);
}


.avatar-uploader {
  border: 1px dashed #0369ad;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 100px;
  height: 100px;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #0068fc;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  border-bottom: 1px solid #5093d2;
  width: 100px;
  height: 100px;
  display: block;
  overflow: no-display;
}
.regdit{
  height: 100%;
  width: 100%;
  background-size:100% 100%;
  background-image: linear-gradient(#926db3, #e8cef5, #c330dc);
  min-height: 100vh;
  justify-content: center;

  align-items: center;
}
.el-input{
  border: 1px solid #13d454;
  border-radius: 6px;
}
.demoForm{
  position: absolute;
  bottom: 0;
  width:100%;
  padding: 0 20px;
  box-sizing: border-box;

}
html,body{
  height: 100%;
}

.labels{
  width: auto;
  top:20px;
  position:absolute;
  left: 44px;
}
</style>
